﻿using AutoMapper;
using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.RBAC;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.RBAC
{
    /// <summary>
    /// 逻辑删除处理命令
    /// </summary>
    public class IsDelCommandHandlter : IRequestHandler<IsDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<PermissionModel> permissionRep;
        private readonly IMapper mapper;

        public IsDelCommandHandlter(IBaseRepository<PermissionModel> PermissionRep,IMapper mapper)
        {
            permissionRep = PermissionRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(IsDelCommand request, CancellationToken cancellationToken)
        {

            var permission = mapper.Map<PermissionModel>(request);
            permission.UpdateTime = DateTime.Now;
            permission.IsDeleted = true;
            await permissionRep.UpdateAsync(permission);

            return new ApiResult<int>()
            {
                code = ResultEnumCode.Ok,
                message = "删除成功",
                data = 1

            };


        }
    }

}
